package com.bdqn.controller;


import com.bdqn.entity.OmsOrder;
import com.bdqn.entity.UmsUser;
import com.bdqn.entity.allParams.SkuInfos;
import com.bdqn.entity.allParams.PreSubmitParam;
import com.bdqn.entity.responseDate.order.PreSubmitDto;
import com.bdqn.entity.util.RespBean;
import com.bdqn.entity.util.ResultEnum;
import com.bdqn.service.OmsOrderService;
import com.bdqn.service.UmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/oms/order")
public class OmsOrderController {
    @Autowired
    private OmsOrderService omsOrderService;
  @Autowired
  private UmsUserService umsUserService;
    /**
     * 由于前台传的可能是多个商品购物信息 所以使用对象数组的方式去接受前台传的参数信息
     * @param preSubmitParam
     * @return
     */
    @PostMapping("/preSubmit")
    public RespBean preSubmit(@RequestBody PreSubmitParam preSubmitParam, HttpServletRequest request) {
        System.out.println("获取到的前台传来的skuInfos数组为"+ Arrays.toString(preSubmitParam.getSkuInfos()));
        SkuInfos[] skuInfos = preSubmitParam.getSkuInfos();
        if(skuInfos==null || skuInfos.length==0){
            return new RespBean(ResultEnum.PARAM_ERROR);
        }
        String token = request.getHeader("token");
        UmsUser userByToken = umsUserService.getUserByToken(token);

        PreSubmitDto preSubmitDto = omsOrderService.preSubmitDto(preSubmitParam.getSkuInfos(),userByToken.getId());

        return  preSubmitDto!=null?RespBean.ok("获取预支付信息成功",preSubmitDto):RespBean.err("获取预支付信息失败");
    }

    /**
     * 添加订单
     * @param preSubmitParam
     * @param request
     * @return
     */
    @PostMapping("/add")
    public RespBean add(@RequestBody PreSubmitParam preSubmitParam,HttpServletRequest request){
        String token = request.getHeader("token");
        UmsUser userByToken = umsUserService.getUserByToken(token);
        preSubmitParam.setUserId(userByToken.getId());
        OmsOrder omsOrder = omsOrderService.addOmsOrder(preSubmitParam);
        return omsOrder!=null?RespBean.ok("创建订单成功",omsOrder):RespBean.err("创建订单失败");
    }

    /**
     * 通过订单编号查询出订单的信息
     * @return
     */
    @GetMapping("/detail/{orderNoD}")
    public RespBean detail(String orderNo,HttpServletRequest request){
         OmsOrder omsOrder=omsOrderService.getOrderByOderNo(orderNo,umsUserService.getUserByToken(request.getHeader("token")).getId());
        return omsOrder!=null?RespBean.ok("获取成功",omsOrder):RespBean.err("获取失败");
    }

    /**
     * 查询用户订单列表
     */
    @GetMapping("/list")
    public RespBean list(HttpServletRequest request){
        //从token中获取userId
        String token = request.getHeader("token");
        UmsUser user = umsUserService.getUserByToken(token);
        List<OmsOrder> list = omsOrderService.getOrderByUserId(user.getId());
        return RespBean.ok("查询订单列表成功", list);
    }
    /**
     * 用户删除订单
     */
    @PostMapping("/del")
    public RespBean list(String orderNo, HttpServletRequest request){
        //从token中获取userId
        String token = request.getHeader("token");
        UmsUser user = umsUserService.getUserByToken(token);
        boolean ok = omsOrderService.delOrder(orderNo, user.getId());
          return ok?RespBean.ok("删除订单成功", null):RespBean.err("删除订单失败");
        }

}
